<?php

/**
 * 下载文件
 */
namespace xzdream;


class Download
{
    //https://www.xzdream.cn/download/创建订单.pdf

    public function downloadPdf($url,&$error){
        $parase_arr = parse_url($url);

        if(!isset($parase_arr['scheme'])){
            $error = "面单路径只支持http/https";
            return "";
        }

        if($parase_arr['scheme'] != "http" && $parase_arr['scheme'] != "https"){
            $error = "面单路径只支持http/https";
            return "";
        }

        $file = @file_get_contents($url);

        if($file === false){
            $error = "不能下载远程路径面单，或者文件无效（0字节）。";
            return "";
        }


        //获取mime type
        $fi = new \finfo(FILEINFO_MIME_TYPE);
        $mime_type = $fi->buffer($file);

        if($mime_type != "application/pdf"){
            $error = "远程路径面单不是有效的PDF文件。";
            return "";
        }

        $base64 = base64_encode($file);

        return $base64;
    }
}
define('_ZT_DEFAULT_ERROR_LEVEL', E_ALL^E_NOTICE ^ E_WARNING );
$down = new Download();
$error = "";
$down->downloadPdf("https://www.xzdream.cn/download/G1267-190522-0013-001.pdf",$error);
echo $error;